package com.asion.qkan.ui.model;

import com.asion.qkan.api.ServiceApi;
import com.asion.qkan.entry.CategoryEntry;
import com.asion.qkan.entry.RecommendEntry;
import com.asion.qkan.ui.contract.FindContract;
import com.asion.qkan.utils.retrofitCache.SetCacheUtils;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by asion on 16-10-12.
 */

public class FindModel implements FindContract.Model {

    @Override
    public void getRecommedDatas(int tag, FindContract.OnGetRecommends onGetRecommends,int type) {
//        http://api.kkmh.com/v1/topic_new/lists/get_by_tag?tag=0&since=20&count=20
        new  Retrofit.Builder().baseUrl("http://api.kkmh.com/")
                .client(SetCacheUtils.setCache())
                .addConverterFactory(GsonConverterFactory.create())
                .build()
                .create(ServiceApi.class)
                .getRecommend(tag)
                .enqueue(new Callback<RecommendEntry>() {
                    @Override
                    public void onResponse(Call<RecommendEntry> call, Response<RecommendEntry> response) {
                        RecommendEntry body = response.body();
                        onGetRecommends.OnRecommedSuccess(body,type);
                    }

                    @Override
                    public void onFailure(Call<RecommendEntry> call, Throwable t) {
                        onGetRecommends.OnFailed(t);
                    }
                });

    }

    @Override
    public void getAllDatas(int tag, FindContract.OnAllDatas onAllDatas) {
        new Retrofit.Builder()
                .baseUrl("http://api.kkmh.com/")
                .client(SetCacheUtils.setCache())
                .addConverterFactory(GsonConverterFactory.create())
                .build()
                .create(ServiceApi.class)
                .getCatagoryData(tag)
                .enqueue(new Callback<CategoryEntry>() {
                    @Override
                    public void onResponse(Call<CategoryEntry> call, Response<CategoryEntry> response) {
                        CategoryEntry body = response.body();
                        onAllDatas.OnAllDatasSuccess(body);
                    }

                    @Override
                    public void onFailure(Call<CategoryEntry> call, Throwable t) {
                        onAllDatas.Failed(t);
                    }
                });
    }
}
